component
{
	property name="beanUtils";
	property name="metadataCache";

	public function init()
	{
		variables.beanUtils = createObject( "component", "com.firemoss.atonement.metadata.beanutils.BeanUtils").init();
		variables.metadataCache = createObject( "java", "java.util.HashMap" ).init();
	}
	
	public function buildMetadata(instance)
	{
		return getMetadata( instance ).name;
	}
	
	public function buildMetadataForClass(clazz)
	{
		if ( not metadataCache.containsKey( clazz ) )
		{
			var md = {};
			
			metadataCache.put( clazz, md );

			buildPropertyMetadata( md, clazz );
		}
		
		return metadataCache.get( clazz );
	}

	private function buildPropertyMetadata( md, clazz)
	{
		var bean =  createObject( "component", clazz.name );
		var beanMd = beanUtils.inspect( bean );
		var propMd = new PropertyMetadata( clazz );
		var key = "";

		var propName = "";
		var propStruct = "";
		var prop = "";
		
		for ( propName in beanMd.properties )
		{
			propStruct = beanMd.properties[ propName ];
			prop = new Property();
			
			prop.setName( propStruct.name );
			prop.setType( propStruct.type );
			
			structKeyExists( propStruct, "valueResolver") ? prop.setValueResolver( propStruct.valueResolver ) : "";
			structKeyExists( propStruct, "valueResolutionFunction") ? prop.setValueResolutionFunction( propStruct.valueResolutionFunction ) : "";
			structKeyExists( propStruct, "ignored") ? prop.setIgnored( propStruct.ignored ) : "";
			structKeyExists( propStruct, "collectionOf") ? prop.setCollectionOf( propStruct.collectionOf ) : "";
			structKeyExists( propStruct, "sourcePropertyName") ? prop.setSourcePropertyName( propStruct.sourcePropertyName ) : "";
			
			propMd.addProperty( prop );
		}		
		
		md.propertyMetadata = propMd;
	}
}
